package com.yhx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yhx.iservice.ISetmealService;
import com.yhx.mapper.CheckgroupMapper;
import com.yhx.mapper.CheckitemMapper;
import com.yhx.mapper.SetmealCheckgroupMapper;
import com.yhx.mapper.SetmealMapper;
import com.yhx.pojo.*;
import com.yhx.service.CheckgroupCheckitemService;
import com.yhx.service.SetmealService;
import com.yhx.utils.*;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Service
public class SetmealServiceImpl implements ISetmealService {
    @Resource
    private SetmealMapper setmealMapper;
    @Resource
    private SetmealService setmealService;
    @Resource
    private CheckgroupCheckitemService checkgroupCheckitemService;
    @Resource
    private CheckgroupMapper checkgroupMapper;
    @Resource
    private CheckitemMapper checkitemMapper;
    @Override
    public PageResult pageList(QueryPageBean queryPageBean) {
        String queryString = queryPageBean.getQueryString();
        System.out.println(queryString);
        QueryWrapper<Setmeal> queryWrapper = new QueryWrapper<>();
        if(queryString!=null || queryString.length()!=0){
            queryWrapper.like("code", queryString)
                    .or().like("name", queryString)
                    .or().like("helpCode", queryString);
        }
        Page<Setmeal> page = new Page<>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<Setmeal> setmealPage = setmealMapper.selectPage(page, queryWrapper);
        return new PageResult(setmealPage.getTotal(), setmealPage.getRecords());
    }
@Resource
private SetmealCheckgroupMapper setmealCheckgroupMapper;

    @Override
    @Transactional
    public Result save(Setmeal setmeal) {
        //如果没有勾选检查组
        List<Integer> list = setmeal.getCheckgroupIds();
        System.out.println(list);
        if(list==null || list.size()==0){
            return new Result(false, MessageConstant.ADD_SETMEAL_FAIL);
        }
        //如果没有助记码
        if(setmeal.getHelpcode()==null || setmeal.getHelpcode().length()==0){
            //设置助记码
            String pinYinHeadChar = PinYin4jUtils.getPinYinHeadChar(setmeal.getName());
            setmeal.setHelpcode(pinYinHeadChar);
        }
        //添加套餐信息
        int insert = setmealMapper.insert(setmeal);
        System.out.println(insert);
        if (insert==0){
            return new Result(false, MessageConstant.ADD_SETMEAL_FAIL);
        }
        //获取主键值
        Integer id = setmeal.getId();
        for (Integer checkgroup : list) {
            SetmealCheckgroup setmealCheckgroup = new SetmealCheckgroup(id, checkgroup);
           setmealCheckgroupMapper.insert(setmealCheckgroup);
        }
        System.out.println("=======");
        return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
    }

    @Override
    public Setmeal getByid(int id) {
        Setmeal setmeal = setmealMapper.selectById(id);
        if(setmeal==null){
            return null;
        }
        //获取主键值
        Integer id1 = setmeal.getId();
        //查询中间表
        QueryWrapper<SetmealCheckgroup> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("setmeal_id", id1);
        List<SetmealCheckgroup> setmealCheckgroups = setmealCheckgroupMapper.selectList(queryWrapper);
        ArrayList<Integer> integers = new ArrayList<>();
        //遍历得到所有的checgroup_id
        for (SetmealCheckgroup setmealCheckgroup : setmealCheckgroups) {
            integers.add(setmealCheckgroup.getCheckgroupId());
        }
        setmeal.setCheckgroupIds(integers);
        return setmeal;
    }

    @Override
    public Result update(Setmeal setmeal) {
        //如果没有勾选检查组
        List<Integer> list = setmeal.getCheckgroupIds();
        if(list==null || list.size()==0){
            return new Result(false, MessageConstant.EDIT_SETMEAL_FAIL);
        }
        //如果没有助记码
        if(setmeal.getHelpcode()==null || setmeal.getHelpcode().length()==0){
            //设置助记码
            String pinYinHeadChar = PinYin4jUtils.getPinYinHeadChar(setmeal.getName());
            setmeal.setHelpcode(pinYinHeadChar);
        }
        //更新套餐信息
        int update = setmealMapper.updateById(setmeal);
        if (update==0){
            return new Result(false, MessageConstant.EDIT_SETMEAL_FAIL);
        }
        //获取主键值
        Integer id = setmeal.getId();
        QueryWrapper<SetmealCheckgroup> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("setmeal_id",id);
        //删除中间表信息
        int delete = setmealCheckgroupMapper.delete(queryWrapper);
        if(delete==0){
            return new Result(false, MessageConstant.EDIT_SETMEAL_FAIL);
        }
        for (Integer checkgroup : list) {
            SetmealCheckgroup setmealCheckgroup = new SetmealCheckgroup(id, checkgroup);
            setmealCheckgroupMapper.insert(setmealCheckgroup);
        }
        return new Result(true, MessageConstant.EDIT_SETMEAL_SUCCESS);
    }

    @Override
    public Result delete(Integer[] ids) {
        List<Integer> integers = Arrays.asList(ids);

        int i = setmealMapper.deleteBatchIds(integers);
        if(i>0){
            return new Result(true, MessageConstant.DELETE_SETMEAL_SUCCESS);
        }
        return new Result(false, MessageConstant.DELETE_SETMEAL_FAIL);
    }

    @Override
    public List<Setmeal> lits() {
        return setmealMapper.selectList(null);
    }

    /**
     * 需要先查看这个套餐是不是存在
     * @param id
     * @return
     */
    @Override
    public Result getSetmeal(int id) {
        Setmeal setmeal = setmealMapper.selectById(id);
        //没有查到，返回查找套餐失败
        if(setmeal==null){
            return new Result(false, MessageConstant.QUERY_SETMEAL_FAIL);
        }
        //这个套餐有什么检查组   先查中间表  再查checkgroup表
        //先查中间表 t_setmeal_checkgroup
        List<SetmealCheckgroup> setmealCheckgroups = setmealService.listBySetmealId(setmeal.getId());
        //查询到了这个套餐所有的检查组的id
        List<Integer> checkgroupIds = getListInteger(setmealCheckgroups);
        //查询这个检查组的所有信息
        //查checkgroup表
        List<Checkgroup> checkgroupList = checkgroupMapper.getByIds(checkgroupIds);
        //查看每个检查组有哪些检查项
        for (Checkgroup checkgroup : checkgroupList) {
            //查询出来的所有中间表信息
            List<CheckgroupCheckitem> list =
                    checkgroupCheckitemService.listByCheckgroupId(checkgroup.getId());
            //通过中间表信息得到所有checkitem的id
            List<Integer> checkitemids = getListInteger(list);
            //查checkitem
            List<Checkitem> checkitemList = checkitemMapper.getByIds(checkitemids);
            //把查出来checkitem的数据存入当前checkgroup对象中
            checkgroup.setCheckitemList(checkitemList);
        }
        //把查出来checkgroup的数据存入当前setmeal对象中
        setmeal.setCheckgroupLists(checkgroupList);
        return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,setmeal);
    }

    @Override
    public  List<Map<String, Object>>  countSetmeal() {
        return setmealMapper.countSetmeal();
    }

    @Override
    public List<Hot> hotSetmeal() {
        return setmealMapper.hotSetmeal();
    }

    /**
     * 解析中间表,并将所有需要的id查出来返回
     * @param list
     * @return
     */
    public List<Integer> getListInteger(List list){
        ArrayList<Integer> ids = new ArrayList<>();
        for (Object object : list) {
            if(object instanceof SetmealCheckgroup){
                SetmealCheckgroup setmealCheckgroup=(SetmealCheckgroup)object;
                ids.add(setmealCheckgroup.getCheckgroupId());
            }
            if(object instanceof CheckgroupCheckitem){
                CheckgroupCheckitem checkgroupCheckitem=(CheckgroupCheckitem)object;
                ids.add(checkgroupCheckitem.getCheckitemId());
            }
        }
        return ids;
    }


}
